package com.grt.condify.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author recivejt
 */
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {

        setFieldVal(metaObject, "crTime", new Date());
        setFieldVal(metaObject, "operTime", new Date());
    }



    @Override
    public void updateFill(MetaObject metaObject) {
        //setFieldVal(metaObject, "operTime", new Date());
        setFieldValByName("operTime", new Date(), metaObject);

    }

    private void setFieldVal(MetaObject metaObject, String fieldName, Object fieldValue) {
        if (null == this.getFieldValByName(fieldName, metaObject)) {
            this.setFieldValByName(fieldName, fieldValue, metaObject);
        }
    }
}
